//
//  AddressAddController.m
//  jang
//
//  Created by lee han on 23/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "AddressAddController.h"
#import "CommonView.h"
#import "CRUserData.h"

@interface AddressAddController ()

@end

@implementation AddressAddController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem.backBarButtonItem setTitle:@""];
    
    _mInputName.delegate = self;
    _mInputPhone.delegate = self;
    _mInputAddress.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)onClickSubmit:(id)sender {
    
    if([[CRUserData sharedInstance] getAddressCount] >= 5){
        [CommonView addAlertView:self Text:@"地址数量已达上限"];
        return;
    }
    
    if(_mInputName.text.length == 0){
        [CommonView addAlertView:self Text:@"请输入姓名"];
        return;
    }
    
    if(_mInputPhone.text.length != 11){
        [CommonView addAlertView:self Text:@"手机号输入有误"];
        return;
    }
    
    
    
    if(_mInputAddress.text.length == 0){
        [CommonView addAlertView:self Text:@"请输入地址"];
        return;
    }
    
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 _mInputName.text,  @"name",
                                 _mInputPhone.text, @"phone",
                                 _mInputAddress.text, @"address",
                                 nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_AddAddress];
}

#pragma mark networkDelegate
- (void)parserResult:(NSDictionary *)response functionType:(NSInteger)type
{
    if([[response objectForKey:@"status"] integerValue] == 1){
        [[CRUserData sharedInstance] setAddressData:[response objectForKey:@"data"]];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        [CommonView addAlertView:self Text:@"添加失败"];
    }
}
//end networkDelegate


#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if(textField == _mInputName){
        [_mInputPhone becomeFirstResponder];
    }
    else if (textField == _mInputPhone){
        [_mInputAddress becomeFirstResponder];
    }
    return YES;
}
//end UITextFieldDelegate

#pragma mark UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){
        [_mInputAddress resignFirstResponder];
        return NO;
    }
    
    return YES;
}
//end UITextViewDelegate
@end
